package com.itheima.task;

import com.itheima.common.utils.AliOssUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Set;

@Component
@Slf4j
public class UploadTask {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private AliOssUtil aliOssUtil;

    @Scheduled(cron = "0/10 * * * * ?")
    public void upload() throws Exception {

        log.info("定时任务执行：清理OSS中垃圾图片");
        Set difference = redisTemplate.opsForSet().difference("upload", "save");

        if (difference != null && !difference.isEmpty()) {
            for (Object o : difference) {
                String url = (String) o;
                aliOssUtil.delete(url);
            }
        }

        redisTemplate.opsForSet().remove("upload",redisTemplate.opsForSet().members("upload"));
        redisTemplate.opsForSet().remove("save",redisTemplate.opsForSet().members("save"));
    }

}
